<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>部门添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" th:href="@{/layuiadmin/layui/css/layui.css}" media="all">
    <style>
        .x-red {
            color: red;
        }
    </style>
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-form-role" id="layuiadmin-form-role" style="padding: 20px 30px 0 0;">
    <div class="layui-form-item">
        <label for="dpName" class="layui-form-label">
            <span class="x-red">*</span>部门名称</label>
        <div class="layui-input-inline">
            <input type="text" id="dpName" name="dpName" lay-verify="required" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="x-red">*</span>上级部门</label>
        <div class="layui-input-inline">
            <select name="dpParentId" lay-verify="required">
                <option value="">请选择上级部门</option>
                <option th:each="d:${departmentList}" th:value="${d.dpId}" th:text="${d.dpName}"></option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="x-red">*</span>店铺权限</label>
        <div class="layui-input-block">
            <input type="checkbox" th:each="shop:${shopList}" th:value="${shop.id}" name="shop" lay-skin="primary"
                   th:title="${shop.shopName}">

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">具体描述</label>
        <div class="layui-input-block">
            <textarea type="text" name="dpDesc" autocomplete="off"
                      class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <button class="layui-btn" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit">
            提交
        </button>
    </div>
</div>

<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form'], function () {
        var $ = layui.$, form = layui.form;

        form.on('submit(layuiadmin-app-form-submit)', function (param) {
            var shopList = [];
            $('input[name=shop]:checked').each(function () {
                shopList.push($(this).val());
            });
            if (shopList.length <= 0) {
                layer.msg("至少选择一个店铺", {icon: 2});
                return;
            }
            param.field.shopList = shopList;
            layer.confirm('是否确认操作？', function (index) {
                console.log(param)
                $.ajax({
                    type: 'POST',
                    url: "add",
                    dataType: "json",
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(param.field),
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg('更新成功', {
                                time: 1000
                            }, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layui.table.reload('LAY-app-content-list');
                                parent.layer.close(index);
                            });

                        } else {
                            layer.msg(res.msg);
                        }
                    },
                    error: function (xhr, type) {
                        layer.msg("系统错误，请联系管理员！");
                    }
                });
            });
            return false;
        });
    })
</script>
</body>
</html>